$(function () {
    $("#jqGrid").jqGrid({
        url: '${request.contextPath}/statics/js/modules/sys/order.json',
        datatype: "json",
        colModel: [
            { label: '产品id', name: 'productId', index: 'productId', width: 40, key: true },
            { label: '套餐月费', name: 'PmonthlyFee', index: 'PmonthlyFee', width: 40 },
            { label: '国内通话', name: 'DomesticCall', index: 'DomesticCall', width: 40 },
            { label: '国内流量', name: 'DomesticFlow', index: 'DomesticFlow', width: 40 },
            { label: '短信条数', name: 'messageNumber', index: 'messageNumber', width: 40 },
            { label: '预存话费', name: 'Predeposit', index: 'Predeposit', width: 40 },
            { label: '实际花费', name: 'costSpeech', index: 'costSpeech', width: 40, key: true },
            { label: '返还话费', name: 'ReturnCharge', index: 'ReturnCharge', width: 40 },
            { label: '返还流量', name: 'ReturnFlow', index: 'ReturnFlow', width: 40 },
            { label: '返还语音', name: 'ReturnSpeech', index: 'ReturnSpeech', width: 40 },
            { label: '提醒短信内容', name: 'warnMessage', index: 'warnMessage', hidden:true },
            { label: '企业代码', name: 'qCode', index: 'qCode', hidden:true },
            { label: '产品代码', name: 'pCode', index: 'pCode'},
            {   label: '扣费类型',
                name: 'koufeiType',
                index: 'koufeiType',
            },
            { label: '费率', name: 'fee', index: 'fee', hidden:true },
            {   label: '订购渠道',
                name: 'orderMethod',
                index: 'orderMethod'
            },
            {   label: '订购',
                name: 'Order',
                index: 'Order',
                width: 40,
                formatter: function (value, grid, rows, state){
                    return '<button class="btn btn-primary" onclick="openModel('+rows.productId+','+rows.warnMessage+','+rows.qCode+','+rows.pCode+','+rows.fee+')">订购</button>'
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});
//订购
function openModel(pid,message,qCode,pCode,fee){
    $('#myModal').modal('show');
    $("#cpid").val(pid);
    $("#warnMessage").val(message);
    $("#qCode").val(qCode);
    $("#pCode").val(pCode);
    $("#fee").val(fee);
}
//操作码
function changeM(select){
    console.log(select);
    var code = $(select).find('option[selected="selected"]').html();
    console.log(code);
    $("#oCode").val(code);
}
//扣费类型
function changeu(select){
    console.log(select);
    var text = $(select).find('option[selected="selected"]').html();
    console.log(text);
    $("#kftype").val(text);
}
//订购渠道
function changeO(select){
    console.log(select);
    var method = $(select).find('option[selected="selected"]').html();
    console.log(method);
    $("#oMethod").val(method);
}
var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        order: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.order = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.order.id == null ? "sys/order/save" : "sys/order/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.order),
                success: function(r){
                    console.log(r);
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/order/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(id){
            $.get(baseURL + "sys/order/info/"+id, function(r){
                vm.order = r.order;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        }
    }
});